import os
import time

from PyQt5.QtGui import QTextCursor
from PyQt5.QtWebEngineWidgets import QWebEngineView

from PyQt5.QtWidgets import QWidget, QTableWidgetItem

from config import config
from PyQt5.QtCore import QTimer, QThread, pyqtSignal, QUrl

from core.QSSLoader import QSSLoader
from views.home import *

class UIHomeView(QWidget, Ui_Form):
    def __init__(self):
        super(UIHomeView, self).__init__()
        self.setupUi(self)
        self.initParams()
        self.initUI()
        self.initSlot()
        self.initMethod()
        self.initAction()

    def initParams(self):
        self.message = QtWidgets.QMessageBox  # 初始化消息弹框，information消息，critical失败

        self.logCount = 1
        self.lastLog = None
        self.image = None
        self.count = 0

    def btnClick(self, status, title, content):
        if status == self.tmsg.code.info:
            self.message.information(self, title, content)
        elif status == self.tmsg.code.error:
            self.message.critical(self, title, content)
        elif status == self.tmsg.code.warning:
            self.message.warning(self, title, content)

    def initAction(self):
        self.scrapyPBClickCallback = None
        self.clearTablePBClickCallback = None
        self.loadLocalDataPBClickCallback = None

        self.createChartPBClickCallback = None
        self.chartParamsPBClickCallback = None

    def initUI(self):
        self.textEdit.setText("")

        self.view = QWebEngineView(self.frame_9)
        self.view.hide()
        sizePolicy = QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.Preferred, QtWidgets.QSizePolicy.Preferred)
        sizePolicy.setHorizontalStretch(1)
        sizePolicy.setVerticalStretch(1)
        sizePolicy.setHeightForWidth(self.view.sizePolicy().hasHeightForWidth())
        self.view.setSizePolicy(sizePolicy)
        self.gridLayout_6.addWidget(self.view, 0, 0, 1, 1)

        self.view_2 = QWebEngineView(self.frame_11)
        self.view_2.hide()
        sizePolicy = QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.Preferred, QtWidgets.QSizePolicy.Preferred)
        sizePolicy.setHorizontalStretch(1)
        sizePolicy.setVerticalStretch(1)
        sizePolicy.setHeightForWidth(self.view_2.sizePolicy().hasHeightForWidth())
        self.view_2.setSizePolicy(sizePolicy)
        self.gridLayout_7.addWidget(self.view_2, 0, 0, 1, 1)

        self.view_3 = QWebEngineView(self.frame_10)
        self.view_3.hide()
        sizePolicy = QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.Preferred, QtWidgets.QSizePolicy.Preferred)
        sizePolicy.setHorizontalStretch(1)
        sizePolicy.setVerticalStretch(1)
        sizePolicy.setHeightForWidth(self.view_3.sizePolicy().hasHeightForWidth())
        self.view_3.setSizePolicy(sizePolicy)
        self.gridLayout_4.addWidget(self.view_3, 0, 0, 1, 1)

        self.view_4 = QWebEngineView(self.frame_8)
        self.view_4.hide()
        sizePolicy = QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.Preferred, QtWidgets.QSizePolicy.Preferred)
        sizePolicy.setHorizontalStretch(1)
        sizePolicy.setVerticalStretch(1)
        sizePolicy.setHeightForWidth(self.view_4.sizePolicy().hasHeightForWidth())
        self.view_4.setSizePolicy(sizePolicy)
        self.gridLayout_3.addWidget(self.view_4, 0, 0, 1, 1)

        self.view_5 = QWebEngineView(self.frame_13)
        self.view_5.hide()
        sizePolicy = QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.Preferred, QtWidgets.QSizePolicy.Preferred)
        sizePolicy.setHorizontalStretch(1)
        sizePolicy.setVerticalStretch(1)
        sizePolicy.setHeightForWidth(self.view_5.sizePolicy().hasHeightForWidth())
        self.view_5.setSizePolicy(sizePolicy)
        self.gridLayout_8.addWidget(self.view_5, 0, 0, 1, 1)

        self.view_6 = QWebEngineView(self.frame_12)
        self.view_6.hide()
        sizePolicy = QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.Preferred, QtWidgets.QSizePolicy.Preferred)
        sizePolicy.setHorizontalStretch(1)
        sizePolicy.setVerticalStretch(1)
        sizePolicy.setHeightForWidth(self.view_6.sizePolicy().hasHeightForWidth())
        self.view_6.setSizePolicy(sizePolicy)
        self.gridLayout_5.addWidget(self.view_6, 0, 0, 1, 1)

        self.view_7 = QWebEngineView(self.frame_14)
        self.view_7.hide()
        sizePolicy = QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.Preferred, QtWidgets.QSizePolicy.Preferred)
        sizePolicy.setHorizontalStretch(1)
        sizePolicy.setVerticalStretch(1)
        sizePolicy.setHeightForWidth(self.view_7.sizePolicy().hasHeightForWidth())
        self.view_7.setSizePolicy(sizePolicy)
        self.gridLayout_9.addWidget(self.view_7, 0, 0, 1, 1)

    def initSlot(self):
        self.pushButton_2.clicked.connect(lambda: self.scrapyPBClick(self.pushButton_2.text()))
        self.pushButton_6.clicked.connect(lambda: self.clearTablePBClickPBClick(self.pushButton_6.text()))
        self.pushButton.clicked.connect(lambda: self.loadLocalDataPBClick(self.pushButton.text()))
        self.light1_swich_PB.clicked.connect(lambda: self.createChartPBClick(1, self.light1_swich_PB.text()))


    def initMethod(self):
        pass

    # region setUI

    def setPyEchartUI(self):
        # 加载 html 文件并在 pyqt 窗口中显示
        fileUrl = config.file

        pieurl = QUrl(f"file:///{fileUrl.echartPath}{fileUrl.pieFileName}.html")
        self.view.load(pieurl)
        self.view.setZoomFactor(0.65)

        barurl = QUrl(f"file:///{fileUrl.echartPath}{fileUrl.barFileName}.html")
        self.view_2.load(barurl)
        self.view_2.setZoomFactor(0.67)

        lineurl = QUrl(f"file:///{fileUrl.echartPath}{fileUrl.lineFileName}.html")
        self.view_3.load(lineurl)
        self.view_3.setZoomFactor(0.65)

        pieurl = QUrl(f"file:///{fileUrl.echartPath}{fileUrl.pieFileName}_2.html")
        self.view_4.load(pieurl)
        self.view_4.setZoomFactor(0.65)

        barurl = QUrl(f"file:///{fileUrl.echartPath}{fileUrl.barFileName}_2.html")
        self.view_5.load(barurl)
        self.view_5.setZoomFactor(0.67)

        lineurl = QUrl(f"file:///{fileUrl.echartPath}{fileUrl.lineFileName}_2.html")
        self.view_6.load(lineurl)
        self.view_6.setZoomFactor(0.65)

        pieurl = QUrl(f"file:///{fileUrl.echartPath}{fileUrl.pieFileName}_3.html")
        self.view_7.load(pieurl)
        self.view_7.setZoomFactor(0.67)

        self.label.hide()
        self.label_3.hide()
        self.label_4.hide()
        self.label_2.hide()
        self.label_5.hide()
        self.label_6.hide()
        self.label_7.hide()

        self.view.show()
        self.view_2.show()
        self.view_3.show()
        self.view_4.show()
        self.view_5.show()
        self.view_6.show()
        self.view_7.show()



    def setTableUI(self, data):
        log = ""
        for key in data:
            log+=f"{key}："
            log+=f"{data[key]}\n"
        log+="\n"
        self.textEdit.append(log)
    # endregion

    # region Slot
    def openCsvPath(self, btnName):
        self.printLog(f"点击了{btnName}按钮")
        dirs = config.file.csvPath
        if not os.path.exists(dirs):
            os.makedirs(dirs)
        os.startfile(dirs)

    def scrapyPBClick(self, btnName):
        self.printLog(f"点击了{btnName}按钮")
        if (btnName == "开始爬取"):
            self.pushButton_2.setText("终止爬取")
            statue = True
        else:
            self.pushButton_2.setText("开始爬取")
            statue = False
        # self.message.information(self, "消息提示", f"点击了{btnName}按钮")
        if self.scrapyPBClickCallback:
            self.scrapyPBClickCallback(statue)

    def clearTablePBClickPBClick(self, btnName):
        self.printLog(f"点击了{btnName}按钮")
        # self.message.information(self, "消息提示", f"点击了{btnName}按钮")

        self.textEdit.setText("")

        self.label.show()
        self.label_3.show()
        self.label_4.show()
        self.label_2.show()
        self.label_5.show()
        self.label_6.show()
        self.label_7.show()

        self.view.hide()
        self.view_2.hide()
        self.view_3.hide()
        self.view_4.hide()
        self.view_5.hide()
        self.view_6.hide()
        self.view_7.hide()
        if self.clearTablePBClickCallback:
            self.clearTablePBClickCallback()

    def loadLocalDataPBClick(self, btnName):
        self.printLog(f"点击了{btnName}按钮")
        # self.message.information(self, "消息提示", f"点击了{btnName}按钮")
        if self.loadLocalDataPBClickCallback:
            self.loadLocalDataPBClickCallback()

    def createChartPBClick(self, index, btnName):
        self.printLog(f"点击了{btnName}按钮")
        # self.message.information(self, "消息提示", f"点击了{btnName}按钮")
        if self.createChartPBClickCallback:
            self.createChartPBClickCallback(index)

    def chartParamsPBClick(self, index, value, btnName):
        self.printLog(f"点击了{btnName}按钮")
        # self.message.information(self, "消息提示", f"点击了btnName按钮")
        if self.chartParamsPBClickCallback:
            self.chartParamsPBClickCallback(index, value)

    # endregion

    def printLog(self, msg):
        if self.lastLog == msg:
            self.logCount = self.logCount + 1
            msg += f' * {self.logCount}'
        else:
            self.logCount = 1
            self.lastLog = msg
        self.message.information(self, "消息", msg)
        if config.log.logShow: print(msg)
